package com.yy.controller;

import com.mysql.cj.jdbc.interceptors.SessionAssociationInterceptor;
import com.yy.pojo.Goods;
import com.yy.pojo.ShoppingCart;
import com.yy.service.GoodsService;
import com.yy.vo.Shopping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class GoodsController {
    @Autowired  //by type
    // @Qualifier("gs1") 注入 id 是 gs1 类型  by  id
    private GoodsService goodsService;

    @RequestMapping("/getAllGoods")
    public String getAllGoods(HttpSession session) {
        List<Goods> list = goodsService.getAllGoods();
//遍历输出list
        session.setAttribute("list", list);
        list.stream().forEach(System.out::println);
        return "index";
    }
    //获取所有商品信息
    @RequestMapping("/getGoodsByConditon")
    public String  getGoodsByConditon(String condition,HttpSession session){

        System.out.println("condition = " + condition);
        List<Goods> list = goodsService.getGoodsByConditon(condition);
        session.setAttribute("list",list);
        //遍历输出
        list.stream().forEach(System.out::println);
        return "index";
    }
    @RequestMapping("/goodsDetails")
    public String getGoodsDetails(String gid, HttpServletRequest request){
        System.out.println(gid);
        Goods goods=goodsService.getGoodsDetails(gid);
        System.out.println(goods);
        request.setAttribute("goods", goods);
        return "goodsDetails";
    }

    @RequestMapping("/addGouwuche")
    public void getGoodsDetails1(String gid, String num,HttpServletResponse response) throws IOException {
        System.out.println(gid);
        System.out.println("num = " + num);
        boolean flag=goodsService.addShoppingCart(gid,num);
        if(flag){
            response.getWriter().write("添加购物车成功");
        }else{
            response.getWriter().write("添加购物车失败");
        }
    }
    @RequestMapping("/shoppingcart")
    public String shoppingCart(HttpSession session){
        List<Shopping> list=goodsService.getShoppingCart();
        session.setAttribute("list", list);
        return "shopping";
    }

}
